package com.example.finaltool.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.finaltool.mapper.AdminAreaMapper;
import com.example.finaltool.model.MyRuntimeException;
import com.example.finaltool.model.db.AdminArea;
import com.example.finaltool.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengbw
 * @date 2021/9/23 2:22 下午
 */
@Slf4j
@Service
public class AdminAreaService {
    AdminAreaMapper mapper;

    @Autowired
    public AdminAreaService(AdminAreaMapper mapper) {
        this.mapper = mapper;
    }

    public Map<String, String> getAreaIds(String shi, String quxian, String zhen, List<String> cunList, File txt) throws IOException {
        AdminArea shiArea = getByName(shi, "0", "210000000000");
        if (shiArea == null)
            shiArea = getByLike(shi, "0", "210000000000");
        if (shiArea == null)
            return emptyAreaMap(cunList);
        AdminArea quArea = getByName(quxian, "1", shiArea.getAreaId());
        if (quArea == null)
            quArea = getByLike(quxian, "1", shiArea.getAreaId());
        if (quArea == null)
            return emptyAreaMap(cunList);
        AdminArea zhenArea = getByName(zhen, "2", quArea.getAreaId());
        if (zhenArea == null)
            zhenArea = getByLike(zhen, "2", quArea.getAreaId());
        if (zhenArea == null) {
            log.info("发现" + shi + quxian + zhen + "未能匹配");
            List<String> notFound = new ArrayList<>(cunList.size() + 1);
            notFound.add(shi + quxian + zhen);
            cunList.forEach(c -> notFound.add(shi + quxian + zhen + c));
            FileUtils.saveNotFoundArea(notFound, txt);
            return emptyAreaMap(cunList);
        }
        Map<String, String> map = new HashMap<>();
        AdminArea finalZhenArea = zhenArea;
        List<String> notFound = new ArrayList<>();
        cunList.forEach(c -> {
            AdminArea cunArea = getByLike(c, "3", finalZhenArea.getAreaId());
            if (cunArea == null) {
                notFound.add(shi + quxian + zhen + c);
            } else map.put(c, cunArea.getAreaId());
        });
        FileUtils.saveNotFoundArea(notFound, txt);
        return map;
    }

    private Map<String, String> emptyAreaMap(List<String> cunList) {
        Map<String, String> map = new HashMap<>(cunList.size());
        cunList.forEach(c -> map.put(c, ""));
        return map;
    }

    private AdminArea getByName(String name, String level, String parentId) {
        QueryWrapper<AdminArea> wrapper = new QueryWrapper<>();
        wrapper.eq("area_name", name)
                .eq("level", level)
                .eq("parent_id", parentId);
        List<AdminArea> areaList = mapper.selectList(wrapper);
        if (areaList.isEmpty())
            return null;
        if (areaList.size() > 1)
            throw new MyRuntimeException("找到的数据不唯一：" + areaList);
        return areaList.get(0);
    }

    private AdminArea getByLike(String name, String level, String parentId) {
        QueryWrapper<AdminArea> wrapper = new QueryWrapper<>();
        wrapper.like("area_name", buildNameFilter(name))
                .eq("level", level)
                .eq("parent_id", parentId);
        List<AdminArea> areaList = mapper.selectList(wrapper);
        if (areaList.isEmpty())
            return null;
        if (areaList.size() > 1)
            throw new MyRuntimeException("找到的数据不唯一：" + areaList);
        return areaList.get(0);
    }

    private String buildNameFilter(String name) {
        char[] nameChars = name.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : nameChars) {
            builder.append("%").append(c);
        }
        return builder + "%";
    }
}
